
/*****************************************************************************
*    Copyright (C)2003 Ali Corporation. All Rights Reserved.
*
*    File:    epg_demo.c
*
*    Description:    This file describes epg application design.
*    History:
*           Date            Athor        Version          Reason
*	    ============	=============	=========	=================
*	1.	Sep.23.2003      Goliath Peng       Ver 0.1    Create file.
*     2.     Jan.19.2004      Goliath Peng       Ver 0,2     Modified the get section mechanism
*	3. 	Aug.12.2004 	   Firs Chen		Ver 1.0	 Modified the eit Data Structure and some api.
*	4. 	Apr.17.2006 	   Sunny Yin		Ver 1.1	 move from rock
*****************************************************************************/
#include <sys_config.h>

#include "com_epg.h"
//#include <api/libtsi/db_3l.h>
//#include <api/libtsi/sec_eit.h>
#include <api/libc/string.h>
//#include<api/libtsi/si_epg.h>
#include<api/libsi/lib_epg.h>
#include "system_data.h"
#include <osal/osal.h>
//#include <api/libosd/osd_common.h>
#include <api/libchar/lib_char.h>

#ifdef EPG_MULTI_LANG
static UINT8 g_current_lang_code[3];

void epg_set_lang_code(UINT8 lang_code[3])
{
	g_current_lang_code[0] = lang_code[0];
	g_current_lang_code[1] = lang_code[1];
	g_current_lang_code[2] = lang_code[2];
}
#endif

#if 0
eit_short_desc_t* epg_get_short_desc(eit_event_info_t *ep)
{
	eit_short_desc_t *p;

	if (ep == NULL)
		return NULL;

	p = ep->eit_sht_desc;
	if(p == NULL)
		return NULL;

	while(p != NULL)
	{
		if(MEMCMP(p->lang_code, g_current_lang_code, 3) == 0)
			break;
		p = p->next;
	}
	if(p == NULL)
		p=ep->eit_sht_desc;

	return p;

}

eit_ext_desc_t* epg_get_ext_desc(eit_event_info_t *ep)
{
	eit_ext_desc_t *p;

	if (ep == NULL)
		return NULL;

	p = ep->eit_ext_desc;
	if(p == NULL)
		return NULL;

	while(p != NULL)
	{
		if(MEMCMP(p->lang_code, g_current_lang_code, 3) == 0)
			break;
		p = p->next;
	}
	if(p==NULL)
		p = ep->eit_ext_desc;
	return p;

}


UINT8* epg_get_event_id(eit_event_info_t *ep,UINT8 *str,INT32 *len)
{
	if (ep==NULL)
		return NULL;

	sprintf(str,"%04d",ep->event_id);
	*len=4;
	str[4]='\0';
	return str;
}

UINT8 *epg_get_event_language(eit_event_info_t *ep)
{
	eit_short_desc_t *p;

	if (NULL==ep)
		return NULL;

#ifdef EPG_MULTI_LANG
	p = epg_get_short_desc(ep);
#else
	p = ep->eit_sht_desc;
#endif

	if(p == NULL)
		return NULL;

	return p->lang_code;
}


UINT8* epg_get_event_name(eit_event_info_t *ep,INT32 *len)
{
	eit_short_desc_t *p;

	*len=0;

	if (ep==NULL)	return NULL;
#ifdef EPG_MULTI_LANG
	p = epg_get_short_desc(ep);
#else
	p = ep->eit_sht_desc;
#endif
	if (p==NULL )	return NULL;

	if (p->event_name==NULL)	return NULL;

	*len=ComUniStrLen((UINT16*)p->event_name);
	return p->event_name;
}

INT32 epg_get_event_detail_flag(eit_event_info_t *ep)
{
	eit_short_desc_t *p;

	if (ep==NULL)
		return 0;

#ifdef EPG_MULTI_LANG
	p = epg_get_short_desc(ep);
#else
	p = ep->eit_sht_desc;
#endif

	if (p==NULL)
		return 0;

	return p->text_desc_flag;

}

UINT8* epg_get_event_detail(eit_event_info_t *ep,INT32 *len)
{
	eit_short_desc_t *p;

	*len=0;

	if (ep==NULL)
		return NULL;

#ifdef EPG_MULTI_LANG
	p = epg_get_short_desc(ep);
#else
	p = ep->eit_sht_desc;
#endif

	if (p==NULL)
		return NULL;

	if (p->text_desc==NULL)
		return NULL;

	*len=ComUniStrLen((UINT16 *)p->text_desc);
	return p->text_desc;
}

//050721-cmchen
INT32 epg_get_event_short_desc_number(eit_event_info_t *ep)
{
	eit_short_desc_t *p,*p0;
	INT16 i=0;
	if (NULL==ep) return 0;

#ifdef EPG_MULTI_LANG
	p = epg_get_short_desc(ep);
	p0 = p;
#else
	p = ep->eit_sht_desc;
#endif

	while (p != NULL)
	{
#ifdef EPG_MULTI_LANG
		if(MEMCMP(p->lang_code, p0->lang_code, 3) != 0)
			break;
#endif

		if(p->text_desc != NULL)
		{
		if (ComUniStrLen((UINT16*)p->text_desc)>0)
				i++;
		}
		p=p->next;
	}

	return i;
}

INT32 epg_get_event_extented_desc_number(eit_event_info_t *ep)
{
	eit_ext_desc_t *p, *p0;
	INT16 i=0;
	if (NULL==ep) return 0;

#ifdef EPG_MULTI_LANG
	p = epg_get_ext_desc(ep);
	p0 = p;
#else
	p = ep->eit_ext_desc;
#endif

	while (p)
	{
#ifdef EPG_MULTI_LANG
		if(MEMCMP(p->lang_code, p0->lang_code, 3) != 0)
			break;
#endif

		if(p->text_char != NULL)
		{
		if (ComUniStrLen((UINT16*)p->text_char)>0)
				i++;
		}
		p=p->next;
	}

	return i;
}

//050721-cmchen
UINT8* epg_get_event_short_detail(eit_event_info_t *ep,INT32 *len,INT32 idx)
{
	INT32 i=0;
	eit_short_desc_t *p,*p0;
	UINT8 *str;
	*len=0;

	if (NULL==ep)
		return NULL;

#ifdef EPG_MULTI_LANG
	p = epg_get_short_desc(ep);
	p0 = p;
#else
	p = ep->eit_sht_desc;
#endif

	if (NULL==p)
		return NULL;
	str=p->text_desc;
	while (i<idx)
	{
		p=p->next;
		if (NULL==p)
			return NULL;

#ifdef EPG_MULTI_LANG
		if(MEMCMP(p->lang_code, p0->lang_code, 3) != 0)
			return NULL;
#endif

		str=p->text_desc;
		i++;
	}

	if(str == NULL)
		return NULL;

	*len=ComUniStrLen((UINT16*)str);

	return str;

}

/*idx -- the idx of extented node */
UINT8* epg_get_event_extented_detail(eit_event_info_t *ep,INT32 *len,INT32 idx)
{

	INT32 i=0;
	eit_ext_desc_t *p,*p0;
	UINT8 *str;
	*len=0;

	if (NULL==ep)
		return NULL;

#ifdef EPG_MULTI_LANG
	p = epg_get_ext_desc(ep);
	p0 = p;
#else
	p = ep->eit_ext_desc;
#endif
	if (NULL==p)
		return NULL;

	str=p->text_char;

	while (i<idx)
	{
		p=p->next;
		if (NULL==p)
			return NULL;
#ifdef EPG_MULTI_LANG
		if(MEMCMP(p->lang_code, p0->lang_code, 3) != 0)
			return NULL;
#endif

		str=p->text_char;
		i++;
	}

	if(str == NULL)
		return NULL;

	*len=ComUniStrLen((UINT16*)str);

	return str;
}

UINT32 epg_get_event_all_short_detail(eit_event_info_t *ep,UINT16 *short_buf,UINT32 maxLen)
{
	UINT32 strlen=0, len;
	UINT16 *p_short=NULL;
	UINT16 *p=short_buf;
	INT32 short_num,i;

	len = 0;
 
	short_num = epg_get_event_short_desc_number(ep);
	for(i=0;i<short_num;i++)
	{
		p_short = (UINT16*)epg_get_event_short_detail(ep,&strlen,i);

		if (p_short!=NULL && len<maxLen-1)
		{
			if (len + strlen > maxLen - 1)
				strlen = maxLen - 1 - len;
			ComUniStrCopyEx(p+len, p_short,strlen);
			len += strlen;
		}		
	}

	return len;

}

UINT32 epg_get_event_all_extented_detail(eit_event_info_t *ep,UINT16 *extented_buf,UINT32 maxLen)
{
	UINT32 strlen=0, len;
	UINT16 *p_extented=NULL;
	UINT16 *p=extented_buf;
	INT32 extented_num,i;

	len = 0;

	extented_num = epg_get_event_extented_desc_number(ep);
	for(i=0;i<extented_num;i++)
	{
		p_extented = (UINT16*)epg_get_event_extented_detail(ep,&strlen,i);

		if (p_extented!=NULL && len<maxLen-1)
		{
			if (len + strlen > maxLen - 1)
				strlen = maxLen - 1 - len;
			ComUniStrCopyEx(p+len, p_extented,strlen);
			len += strlen;
		}		
	}

	return len;

}

INT32 ConvertDateTimeToString(date_time *dt, UINT8* str)
{
	UINT8 hour,min;
	INT32 hour_off,min_off,sec_off;
	date_time tmp;

	SYSTEM_DATA * sys_data;

	get_STC_offset(&hour_off,&min_off,&sec_off);

	convert_time_by_offset(&tmp, dt,  hour_off, min_off);

	hour = tmp.hour;
	min = tmp.min;

 	sprintf(str, "%02d:%02d",hour, min);
 	return 5;
}

INT32 epg_get_event_start_time(eit_event_info_t *ep,UINT8* str)
{
	if (ep==NULL)
		return 0;
	return ConvertDateTimeToString(&ep->start_time, str);
	}

INT32 epg_get_event_end_time(eit_event_info_t *ep,UINT8* str)
	{
	if (ep==NULL)
		return 0;
	return ConvertDateTimeToString(&ep->end_time, str);
	}


INT32 epg_get_event_time(int nType, eit_event_info_t *ep,UINT8* str)
{
	INT32 nLen=0;

	if (ep!=NULL)
	{
		nLen = epg_get_event_start_time(ep, str);
		if (nType==0)
		{
		str[nLen++]='-';
		nLen += epg_get_event_end_time(ep, str+nLen);
	}
	}
	str[nLen]=0;
	return nLen;
}

INT32 epg_get_event_rating(eit_event_info_t* ep,UINT8* str,INT32 *len)
{
	UINT32 rating=0;

	if (ep==NULL) return 0;

	if (NULL==ep->eit_pr_desc)
	{
		rating=0;
	}
	else
		rating=ep->eit_pr_desc->rating;

	if (rating>0)
		rating+=3;

	sprintf(str,"%02d",rating);
	*len=2;
	str[*len]='\0';
	return SUCCESS;
}

INT32 epg_get_event_content_type(eit_event_info_t* ep,UINT8 *str,INT32 *len)
{
	if (ep==NULL) return 0;
	if (NULL==ep->eit_ct_desc)
	{
		*len=5;
		STRCPY(str,"Other");
		str[5]='\0';
		return !SUCCESS;
	}

	STRCPY(str, content_nibble[ep->eit_ct_desc->content_nibble1]);
	*len=STRLEN(content_nibble[ep->eit_ct_desc->content_nibble1]);
	str[*len]='\0';
	return SUCCESS;
}


#ifdef	PARSING_EXTRA_DESC_EN
INT32 epg_get_event_ca_id(eit_event_info_t *ep,UINT8 *str,INT32 *len)
{
	*len=0;

	if (ep==NULL)
		return NULL;
	if (NULL==ep->eit_ca_desc)
		return !SUCCESS;

	//get_digit_string(str, ep->eit_ca_desc->ca_system_id,4,'0');
	sprintf(str,"%04d",ep->eit_ca_desc->ca_system_id);
 	*len=4;
	str[*len]='\0';
	return SUCCESS;
}

INT32 epg_get_event_component(eit_event_info_t *ep,UINT8 *str,INT32 *len)
{
	eit_component_desc_t *eit_component_desc;
	*len=0;

	if (ep==NULL)
		return NULL;
	eit_component_desc=ep->eit_component_desc;
	if (NULL== eit_component_desc)
		return !SUCCESS;

	if (eit_component_desc->stream_type>0 && eit_component_desc ->stream_type<3)	/* 1 video , 2 audio */
	{
		sprintf(str,"%s",stream_type[eit_component_desc->stream_type][eit_component_desc->component_type]);
		*len=STRLEN(stream_type[eit_component_desc->stream_type][eit_component_desc->component_type]);
	}

	return SUCCESS;
}
#endif

#ifdef TEST
static void epg_printf_event(eit_event_info_t *ep)
{
	if (ep==NULL)
		soc_printf("\tevent empty!\n");
	else
	{
		soc_printf("\tevent: 0x%x,  running status=0x%x  ",ep->event_id,ep->running_status);
		if (ep->eit_sht_desc->event_name==NULL)
			soc_printf("\n");
		else
			soc_printf("\tevent_name: %s  start=%04d/%02d/%02d %02d:%02d  end=%04d/%02d/%02d %02d:%02d\n",ep->eit_sht_desc->event_name,ep->start_time.year,ep->start_time.month,ep->start_time.day,ep->start_time.hour,ep->start_time.min,ep->end_time.year,ep->end_time.month,ep->end_time.day,ep->end_time.hour,ep->end_time.min);
		if (ep->eit_pr_desc==NULL)
			soc_printf("rating==NULL\n");
		else
			soc_printf("rating==%d\n",ep->eit_pr_desc->rating);
		if (ep->eit_ct_desc==NULL)
			soc_printf("content==NULL\n");
		else
			soc_printf("content_id=%d,content=%s\n",ep->eit_ct_desc->content_nibble1,content_nibble[ep->eit_ct_desc->content_nibble1]);	}
}

static void epg_list_service(eit_service_info_t *sp)
{
	eit_event_info_t *ep=sp->pf_event_header;
	osal_task_dispatch_off();

	soc_printf(" service: 0x%x\n  sch_cur_section[0]=%d\n  sch_cur_section[1]=%d \n",sp->service_id,sp->sch_cur_sec_num[0],sp->sch_cur_sec_num[1]);
	soc_printf("--------------------------------pf event------------------------------------------------------------------------------\n");
	while (ep!=NULL)
	{
		epg_printf_event(ep);
		ep=ep->next;
	}
	ep=sp->sch_event_header[0];
	soc_printf("--------------------------------schedule event table_id 0x50--------------------------------\n");
	while(ep!=NULL)
	{
		epg_printf_event(ep);
		ep=ep->next;
	}
	ep=sp->sch_event_header[1];
	soc_printf("--------------------------------schedule event table_id 0x51--------------------------------\n");
	while(ep!=NULL)
	{
		epg_printf_event(ep);
		ep=ep->next;
	}
	ep=sp->sch_event_header[2];
	soc_printf("--------------------------------schedule event table_id 0x52--------------------------------\n");
	while(ep!=NULL)
	{
		epg_printf_event(ep);
		ep=ep->next;
	}
	ep=sp->sch_event_header[3];
	soc_printf("--------------------------------schedule event table_id 0x53--------------------------------\n");
	while(ep!=NULL)
	{
		epg_printf_event(ep);
		ep=ep->next;
	}
	soc_printf("---------------------------------------------------------------------------------------------------------------------\n\n");
	osal_task_dispatch_on();

}

#endif

static eit_event_info_t* epg_get_present_event(eit_service_info_t *eit_service_header)
{
	eit_event_info_t *ep=eit_service_header->pf_event_header;
	while(ep!=NULL)
	{
		if (!(ep->running_status & 0x80))
			break;
		else
			ep=ep->next;
	}
	return ep;
}

static eit_event_info_t* epg_get_following_event(eit_service_info_t *eit_service_header)
{
	eit_event_info_t *ep=eit_service_header->pf_event_header;
	while(ep!=NULL)
	{
		if (ep->running_status & 0x80)
			break;
		else
			ep=ep->next;
	}
	return ep;
}

extern INT32	day_offset;

eit_event_info_t *sch_next_event_header_addr;
static eit_event_info_t* epg_get_schdedule_event(eit_service_info_t *eit_service_header,date_time *sch_time,INT32* event_number)
{

	date_time cur_time,day_start,day_end,tmp,tmp_start,tmp_end;
	eit_event_info_t *start_evt=NULL,*ep=NULL, *tmp_ep=NULL;
	int i=0,day;
	INT32 hoff,moff,soff;
	SYSTEM_DATA* sys_data;
	UINT8 sch_day;
#ifdef GET_TOTAL_SCH_EPG //only changed for shenzhou,do not filter the event UI wanted
	ep=eit_service_header->sch_event_header[0];

	while (ep!=NULL)
	{
		if (eit_compare_time(&ep->start_time,sch_time)>=0 &&  //
			eit_compare_time(&ep->end_time,sch_time)<0)
			break;
		else if(eit_compare_time(&ep->start_time,sch_time)<=0)
			break;
		ep=ep->next;
	}
	start_evt=ep;

	while (ep!=NULL )
	{
		ep=ep->next;
		i++;
	}

	*event_number=i;

	return start_evt;
#endif
	 sys_data = sys_data_get();

	//if (is_time_inited()==FALSE)
	//	eit_get_schedule_time(&cur_time);
	//else
	{
		if(sys_data->local_time.buseGMT)
		{
			get_UTC(&cur_time);
			get_STC_offset(&hoff,&moff,&soff);
		}
		else
		{
			get_STC_time(&cur_time);
			hoff=moff=soff=0;
		}

	}

	/*change sch_time -> UTC time*/

	tmp=*sch_time;

	if (eit_compare_time(&tmp,&cur_time)>0)
		return NULL;


	day=relative_day(&cur_time, &tmp);
	if (day>=0 && day < SCHEDULE_DAY)
	{
		sch_day = day/4;
		ep=eit_service_header->sch_event_header[day/4];
		if((sch_day+1) < SCHEDULE_TABLE_ID_NUM)
		{
			tmp_ep = ep;
			while(tmp_ep!=NULL)
			{
				if((tmp_ep->next)==NULL)
					break;
				tmp_ep = tmp_ep->next;
			}
			sch_next_event_header_addr = tmp_ep->next=eit_service_header->sch_event_header[sch_day+1];
		}

	}
	else
	{
		return NULL;
	}

	convert_time_by_offset(&day_start, &tmp,  hoff, moff);
	day_end = day_start;
	day_end.hour = 23;
	day_end.min = 59;
#ifndef EPG_SHOW_BYTIME
	day_start.hour = 0;
	day_start.min = 0;
#endif
	//soc_printf(" date_start:%04d/%02d/%02d %02d:%02d \n",day_start.year,day_start.month,day_start.day,day_start.hour,day_start.min);
	//soc_printf(" day_end:%04d/%02d/%02d %02d:%02d \n",day_end.year,day_end.month,day_end.day,day_end.hour,day_end.min);
	while(ep!=NULL)
	{
		convert_time_by_offset(&tmp_start, &(ep->start_time),  hoff, moff);
		convert_time_by_offset(&tmp_end,&(ep->end_time),  hoff, moff);
		//soc_printf(" tmp_start:%04d/%02d/%02d %02d:%02d \n",tmp_start.year,tmp_start.month,tmp_start.day,tmp_start.hour,tmp_start.min);
		//soc_printf(" tmp_end:%04d/%02d/%02d %02d:%02d \n",tmp_end.year,tmp_end.month,tmp_end.day,tmp_end.hour,tmp_end.min);
		if ((eit_compare_time(&tmp_start,&day_start)<=0) && (eit_compare_time(&tmp_end,&day_end)>0))
			break;
		ep=ep->next;
	}
	start_evt=ep;
	while(ep!=NULL)
	{
		convert_time_by_offset(&tmp_start,&(ep->start_time),  hoff, moff);
		//soc_printf(" !!!!tmp_start:%04d/%02d/%02d %02d:%02d \n",tmp_start.year,tmp_start.month,tmp_start.day,tmp_start.hour,tmp_start.min);
		if (eit_compare_time(&tmp_start,&day_end)<0)
			break;
		ep=ep->next;
		i++;
	}
	//convert_time_by_offset(&tmp_start,&(ep->start_time),  hoff, moff);
	//soc_printf(" !!!!next_start:%04d/%02d/%02d %02d:%02d \n",tmp_start.year,tmp_start.month,tmp_start.day,tmp_start.hour,tmp_start.min);
	*event_number=i;
	tmp_ep->next = NULL;
	return start_evt;

}


static eit_event_info_t *epg_get_event(eit_tp_t* header,
						UINT32 orig_network_id,UINT16 ts_id,UINT16 service_id,
						UINT32 event_type,date_time* date,
						INT32 *event_num)
{
	eit_tp_t *tp=NULL;
	eit_event_info_t *ep=NULL;
	eit_service_info_t *sp=NULL;

	tp=epg_get_tp(header,orig_network_id,ts_id);
	if (event_num != NULL)
		*event_num=0;
	if (NULL==tp)
		return NULL;

	sp=epg_get_service(tp,service_id);
	if (NULL== sp)
		return NULL;



	if (event_type==PRESENT_EVENT)
	{
		ep=epg_get_present_event(sp);
#ifdef TEST
		epg_list_service(sp);
#endif
	}
	else if  (event_type==FOLLOWING_EVENT)
		ep=epg_get_following_event(sp);
	else
	{
#ifdef TEST
		epg_list_service(sp);
#endif
		ep=epg_get_schdedule_event(sp,date,event_num);
	}
	return ep;	
}
eit_event_info_t* epg_get_first_event(INT32 prog_idx, UINT32 event_type,date_time *dt,INT32 *event_num)
{
	P_NODE service_info;
	T_NODE tp_info;
	UINT16 service_id,orig_nw_id,ts_id;
	eit_tp_t *tp=NULL;
	eit_event_info_t *ep=NULL;
	eit_service_info_t *sp=NULL;
		

	
	if (SUCCESS==get_prog_at(prog_idx,&service_info))
	{
		service_id=service_info.prog_number;
		get_tp_by_id(service_info.tp_id, &tp_info);
		orig_nw_id=tp_info.network_id;
		ts_id=tp_info.t_s_id;

#ifdef EPG_NETWORK_PATCH
		tp=epg_get_tp(get_tp_header(),service_info.tp_id,ts_id);
#else
		tp=epg_get_tp(get_tp_header(),orig_nw_id,ts_id);
#endif
		*event_num=0;
		if (NULL==tp)
			return NULL;

		sp=epg_get_service(tp,service_id);
		if (NULL== sp)
			return NULL;
		if (event_type==PRESENT_EVENT)
		{
			ep=epg_get_present_event(sp);
		}	
		else if  (event_type==FOLLOWING_EVENT)
			ep=epg_get_following_event(sp);
		else
		{
//			ep=epg_get_schdedule_event(sp,date,event_num);
			ep=sp->sch_event_header[0];
		}			
		return ep;
		//		return epg_get_event(get_tp_header(), orig_nw_id, ts_id, service_id, event_type, dt, event_num);
		
	}
	else	
		return NULL;		
}
eit_event_info_t* epg_get_cur_service_event(INT32 prog_idx, UINT32 event_type,date_time *dt,INT32 *event_num)
{
	P_NODE service_info;
	T_NODE tp_info;
	UINT16 service_id,orig_nw_id,ts_id;
	eit_tp_t tp;
	
	if (SUCCESS==get_prog_at(prog_idx,&service_info))
	{
		service_id=service_info.prog_number;
		get_tp_by_id(service_info.tp_id, &tp_info);
		orig_nw_id=tp_info.network_id;
		ts_id=tp_info.t_s_id;

#ifdef EPG_NETWORK_PATCH
		return epg_get_event(get_tp_header(), service_info.tp_id, ts_id, service_id, event_type, dt, event_num);
#else
		return epg_get_event(get_tp_header(), orig_nw_id, ts_id, service_id, event_type, dt, event_num);
#endif		
	}
	else	
		return NULL;		
}
#else
static char *content_nibble[16]=
{
	" ",
	"Movie",
	"News",
	"Show/Game",
	"Sports",
	"Childen's",
	"Music",
	"Arts/Culture",
	"Social/Eco",
	"Edu/Science",
	"Leisure",
	"Other",
	"Other",
	"Other",
	"Other",
	"Other"
};

static UINT8 epg_lang_select = 0;	//for multi-language
/*
 * lang_code2_select - user currently selected language
 * epg_lang_select is not recommended to use anymore
 */
static UINT16 lang_code2_select = 0xFFFF;

#define MAX_EVENT_NAME_LEN			52	//32		//Ben 131008#1
static UINT16 p_event_name[MAX_EVENT_NAME_LEN+1];
static UINT16 f_event_name[MAX_EVENT_NAME_LEN+1];
static UINT16 sch_event_name[MAX_EVENT_NAME_LEN+1];
UINT8* epg_get_event_name(eit_event_info_t *ep, INT32 *name_len)
{
	UINT8 *name;
	UINT8 len = 0;
	UINT32 refcode;
	UINT8 lang_select;
	
	if (ep==NULL)
		return NULL;
	lang_select = epg_get_event_language_select(ep);

	if (ep->event_type == PRESENT_EVENT)
		name = (UINT8*)p_event_name;
	else if (ep->event_type == FOLLOWING_EVENT)
		name = (UINT8*)f_event_name;
	else
		name = (UINT8*)sch_event_name;

	//select language
#ifdef EPG_MULTI_LANG
	for (epg_lang_select=0; epg_lang_select<MAX_EPG_LANGUAGE_COUNT; epg_lang_select++)
	{
		if (compare_iso639_lang(lang2tolang3(ep->lang[epg_lang_select].lang_code2), g_current_lang_code) == 0)
			break;
	}
	if (epg_lang_select >= MAX_EPG_LANGUAGE_COUNT)
		epg_lang_select = 0;
#endif

	if (ep->lang[lang_select].text_char == NULL)
	{
		dvb_to_unicode("No Name", 7, (UINT16 *)name, MAX_EVENT_NAME_LEN, 1);
		return name;
	}

    if(NULL != ep->lang[lang_select].text_char)
	    len = ep->lang[lang_select].text_char[0];
	if (len > 0)
	{
		refcode = get_iso639_refcode(lang2tolang3(ep->lang[lang_select].lang_code2));
    *name_len = dvb_to_unicode(ep->lang[lang_select].text_char+1, len, (UINT16*)name, MAX_EVENT_NAME_LEN,refcode);  
		return name;
	}
	else
	{
		dvb_to_unicode("No Name", 7, (UINT16 *)name, MAX_EVENT_NAME_LEN, 1);
		return name;
	}
}

static INT32 ConvertDateTimeToString(date_time *dt, UINT8* str)
{
	UINT8 hour,min;
	INT32 hour_off,min_off,sec_off;
	date_time tmp;

	SYSTEM_DATA * sys_data;
	
	get_STC_offset(&hour_off,&min_off,&sec_off);

	convert_time_by_offset(&tmp, dt,  hour_off, min_off);

	hour = tmp.hour;
	min = tmp.min;

 	sprintf(str, "%02d-%02d %02d:%02d",tmp.month, tmp.day, hour, min);
 	return 5;
}

INT32 epg_get_event_start_time(eit_event_info_t *ep,UINT8* str)
{
	date_time dt;

	if (ep==NULL)
		return 0;

	get_event_start_time(ep, &dt);

	return ConvertDateTimeToString(&dt, str);
}

INT32 epg_get_event_end_time(eit_event_info_t *ep,UINT8* str)
{
	date_time dt;

	if (ep==NULL)
		return 0;

	get_event_end_time(ep, &dt);
	
	return ConvertDateTimeToString(&dt, str);
}

UINT8 *epg_get_event_language(eit_event_info_t *ep)
{
	UINT8 lang_select;
	if (NULL==ep)
		return NULL;

	lang_select = epg_get_event_language_select(ep);
	return lang2tolang3(ep->lang[lang_select].lang_code2);
}
UINT8 epg_get_event_language_count(eit_event_info_t *ep)
{
	UINT8 i, count = 0;
	if (ep == 0)
		return 0;
	for (i=0; i<3; i++) 
		if (ep->lang[i].lang_code2 != 0)
			count++;
	return count;	
}

UINT8 epg_get_event_language_select(eit_event_info_t *ep)
{
	UINT8 i;
	if (lang_code2_select == 0xFFFF)
		return 0;
	
	for (i=0; i<MAX_EPG_LANGUAGE_COUNT; i++)
		if (ep->lang[i].lang_code2 == lang_code2_select)
			return i;
		
	return 0;
}

void epg_set_event_langcode_select(UINT16 lang_code2)
{
	lang_code2_select = lang_code2;
}

INT32 epg_get_event_content_type(eit_event_info_t* ep,UINT8 *str,INT32 *len)
{
	if (ep==NULL) return !SUCCESS;

	STRCPY(str, content_nibble[ep->nibble]);
	*len=STRLEN(content_nibble[ep->nibble]);
	str[*len]='\0';

	return SUCCESS;
}

UINT32 epg_get_event_all_short_detail(eit_event_info_t *ep,UINT16 *short_buf,UINT32 maxLen)
{
	UINT8* text;
	UINT16 len;
	UINT8 lang_select;

	if (ep==NULL)	return 0;

	lang_select = epg_get_event_language_select(ep);
	if (ep->lang[lang_select].text_char == NULL)	return 0;

	len = ep->lang[lang_select].text_char[0];			//event name
	text = ep->lang[lang_select].text_char + len + 1;	//short text char
	len = text[0];
	if (len > 0)
	{
		return dvb_to_unicode(text+1, len, short_buf, maxLen,
							get_iso639_refcode(lang2tolang3(ep->lang[lang_select].lang_code2)));
	}
	else
		return 0;
}
UINT32 epg_get_event_all_extented_detail(eit_event_info_t *ep,UINT16 *extented_buf,UINT32 maxLen)
{
	UINT8* text;
	UINT16 len;
	UINT8 lang_select;

	if (ep==NULL)	return 0;
	lang_select = epg_get_event_language_select(ep);

	if (ep->lang[lang_select].text_char == NULL)	return 0;
	
#ifdef EPG_MULTI_TP
	len = ep->lang[lang_select].ext_text_length;
	text = ep->lang[lang_select].ext_text_char;
	if (len > 0)
	{
		return dvb_to_unicode(text, len, extented_buf, maxLen,
							get_iso639_refcode(lang2tolang3(ep->lang[lang_select].lang_code2)));
	}
	else
		return 0;
#else // for multi tp epg, by davy
	len = ep->lang[lang_select].text_char[0];			//event name
	text = ep->lang[lang_select].text_char + len + 1;	//short text char
	len = text[0];
	text += (len + 1);				//extended text char
	len = (text[0]<<8) | text[1];
	if (len > 0)
	{
		return dvb_to_unicode(text+2, len, extented_buf, maxLen,
							get_iso639_refcode(lang2tolang3(ep->lang[lang_select].lang_code2)));
	}
	else
		return 0;
#endif
}

eit_event_info_t* epg_get_cur_service_event(INT32 prog_idx, UINT32 event_type,date_time *start_dt,date_time *end_dt,INT32 *event_num, BOOL update)
{
	P_NODE service_info;
	date_time cur_dt;

	if (event_num != NULL)
		*event_num = 0;

	if (SUCCESS==get_prog_at(prog_idx, &service_info))
	{
#ifdef EPG_MULTI_TP
		get_local_time( &cur_dt );
		epg_set_select_time(&cur_dt, start_dt, end_dt);
#endif
		return epg_get_service_event(service_info.tp_id, service_info.prog_number,
									event_type, start_dt, end_dt, event_num, update);
	}
	else
		return NULL;
}
#endif
